
package com.probiz.estore.sales.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.sales.SalesConstants;
import com.probiz.estore.sales.dao.AutoEvalRecommendationDao;
import com.probiz.estore.sales.service.AlsoBuyManager;
import com.probiz.estore.sales.service.AutoEvalRecommendationManager;


public class AutoEvalRecommendationManagerImpl extends GenericManagerImpl<Product> implements AutoEvalRecommendationManager
{
	private AutoEvalRecommendationDao autoEvalRecommendationDao;
	
	private AlsoBuyManager  alsoBuyManager;
	
	public void setAutoEvalRecommendationDao(
			AutoEvalRecommendationDao autoEvalRecommendationDao) {
		this.autoEvalRecommendationDao = autoEvalRecommendationDao;
	}
	


	public void setAlsoBuyManager(AlsoBuyManager alsoBuyManager) {
		this.alsoBuyManager = alsoBuyManager;
	}



	protected void initManager() {
		dao = autoEvalRecommendationDao;
	}

	
	protected void preDelete(Product entity) {

	}

	protected void preSave(Product entity) {

	}
	
	// 自动评估 根据[推荐规则code][源Id]获得特定范围的推荐产品(不能重复,而且必须是激活的)
	public List<Product> getProductsByRuleCodeBySourceId(
			final Integer ruleCode, final Integer sourceId,
			final int firstResult, final int maxResults) {
		switch (ruleCode.intValue()) {
			case SalesConstants.RECOMMENDED_RULE_CODE_NONE:
				return new ArrayList<Product>();
			case SalesConstants.RECOMMENDED_RULE_CODE_BUY_COUNT:
				return this.getBuyCountProductsBySourceId(sourceId, firstResult, maxResults);
			case SalesConstants.RECOMMENDED_RULE_CODE_NEW:	
				return this.getNewProductsBySourceId(sourceId, firstResult, maxResults);
			case SalesConstants.RECOMMENDED_RULE_CODE_ALSO_BUY:	
				return this.getAlsoBuyProductsBySourceId(sourceId, firstResult, maxResults);
			case SalesConstants.RECOMMENDED_RULE_CODE_SIMILAR_PRODUCT:
				return new ArrayList<Product>();
			case SalesConstants.RECOMMENDED_RULE_CODE_SIMILAR_PRICE:	
				return this.getSimilarPriceProductsBySourceId(sourceId, firstResult, maxResults);
			case SalesConstants.RECOMMENDED_RULE_CODE_SAME_BRAND:	
				return this.getSameBrandProductsBySourceId(sourceId, firstResult, maxResults);
			default:
				return new ArrayList<Product>();
		}
	}

	//TODO 目前只是做了AlsoBuy部分，如果需要可再扩充。
	public List<Product> getProductsByRuleCodeBySourceIdList(
			final Integer ruleCode, final List<Integer> sourceIds,
			final int firstResult, final int maxResults){
		switch (ruleCode.intValue()) {
			case SalesConstants.RECOMMENDED_RULE_CODE_NONE:
				return new ArrayList<Product>();
			case SalesConstants.RECOMMENDED_RULE_CODE_BUY_COUNT:
				if(CollectionUtils.isNotEmpty(sourceIds)){
					//获取第一个就满足，因为不会处于多个目录
					return this.getBuyCountProductsBySourceId(sourceIds.get(0), firstResult, maxResults);
				}else{
					return new ArrayList<Product>();
				}	
			case SalesConstants.RECOMMENDED_RULE_CODE_NEW:	
				if(CollectionUtils.isNotEmpty(sourceIds)){
					//获取第一个就满足，因为不会处于多个目录
					return this.getNewProductsBySourceId(sourceIds.get(0), firstResult, maxResults);
				}else{
					return new ArrayList<Product>();
				}	
			case SalesConstants.RECOMMENDED_RULE_CODE_ALSO_BUY:	
				return this.getAlsoBuyProductsBySourceIdList(sourceIds, firstResult, maxResults);
			case SalesConstants.RECOMMENDED_RULE_CODE_SIMILAR_PRODUCT:
				return new ArrayList<Product>();
			case SalesConstants.RECOMMENDED_RULE_CODE_SIMILAR_PRICE:	
				if(CollectionUtils.isNotEmpty(sourceIds)){
					//获取第一个就满足，因为不会获取多个商品的相似价格商品
					return this.getSimilarPriceProductsBySourceId(sourceIds.get(0), firstResult, maxResults);
				}else{
					return new ArrayList<Product>();
				}	
			case SalesConstants.RECOMMENDED_RULE_CODE_SAME_BRAND:	
				if(CollectionUtils.isNotEmpty(sourceIds)){
					//获取第一个就满足，因为不会获取多个商品的同品牌商品
					return this.getSameBrandProductsBySourceId(sourceIds.get(0), firstResult, maxResults);
				}else{
					return new ArrayList<Product>();
				}	
			default:
				return new ArrayList<Product>();
		}
	}
	

	public List<Product> getBuyCountProductsBySourceId(final Integer sourceId,
			final int firstResult, final int maxResults){
		return autoEvalRecommendationDao.getBuyCountProductsBySourceId(sourceId, firstResult, maxResults);
	}

	public List<Product> getNewProductsBySourceId(final Integer sourceId,
			final int firstResult, final int maxResults){
		return autoEvalRecommendationDao.getNewProductsBySourceId(sourceId, firstResult, maxResults);
	}

	public List<Product> getSimilarPriceProductsBySourceId(
			final Integer sourceId, final int firstResult, final int maxResults){
		return autoEvalRecommendationDao.getSimilarPriceProductsBySourceId(sourceId, firstResult, maxResults);
	}

	public List<Product> getSameBrandProductsBySourceId(final Integer sourceId,
			final int firstResult, final int maxResults){
		return autoEvalRecommendationDao.getSameBrandProductsBySourceId(sourceId, firstResult, maxResults);
	}
	public List<Product> getAlsoBuyProductsBySourceId(final Integer sourceId,
			final int firstResult, final int maxResults){
		return alsoBuyManager.getProductsBySourceId(sourceId, firstResult, maxResults);
	}
	public List<Product> getAlsoBuyProductsBySourceIdList(final List<Integer> sourceIds,
			final int firstResult, final int maxResults){
		return alsoBuyManager.getProductsBySourceIdList(sourceIds, firstResult, maxResults);
	}
}
